#!/bin/bash

deploy_jhipster-registry(){
    # jhipster-registry
    docker stack deploy -c ./jhipster-registry.yaml cluster
}

deploy_uaa(){
    # uaa
    mkdir -p /swarm/volumes/zk-{1..3}
    docker stack deploy -c ./uaa.yaml cluster
}

deploy_hadoop(){
    # hadoop
    docker stack deploy -c ./hadoop.yaml cluster
}

deploy_hbase(){
    # hbase
    docker stack deploy -c ./hbase.yaml cluster
}

deploy_kafka(){
    # kafka
    docker stack deploy -c ./kafka.yaml cluster
}

deploy_opentsdb(){
    # opentsdb
    docker stack deploy -c ./opentsdb.yaml cluster
}

deploy_storm(){
    # storm
    docker stack deploy -c ./storm.yaml cluster
}

deploy_hazelcast(){
    # hazelcast
    docker stack deploy -c ./hazelcast.yaml cluster
}

deploy_mongo(){
    # mongo
    docker stack deploy -c ./mongo.yaml cluster
}

deploy_mysql(){
    # mysql
    docker stack deploy -c ./mysql.yaml cluster
}

deploy_fastdfs(){
    # fastdfs
    docker stack deploy -c ./fastdfs.yaml cluster
}

deploy_redis(){
    # redis
    docker stack deploy -c ./redis.yaml cluster
}

delete_jhipster-registry(){
    # delete jhipster-registry
    docker service rm cluster_jhipster-gegistry
    docker service rm cluster_jhipster-registry-1 cluster_jhipster-registry-2
}

delete_uaa(){
    # delete uaa
    docker service rm cluster_uaa
    docker service rm cluster_uaa-1 cluster_uaa-2
}

delete_hadoop(){
    # delete hadoop
    docker service rm cluster_hadoop-hdfs-dn-1 cluster_hadoop-hdfs-dn-2 cluster_hadoop-hdfs-dn-3
}

delete_hbase(){
    # delete hbase
    docker service rm cluster_hbase-master-1 cluster_hbase-master-2 cluster_hbase-rs-1 cluster_hbase-rs-2 cluster_hbase-rs-3
}

delete_kafka(){
    # delete kafka
    docker service rm cluster_kafka-1 cluster_kafka-2 cluster_kafka-3
}

delete_opentsdb(){
    # delete opentsdb
    docker service rm cluster_opentsdb
}

delete_storm(){
    # storm
    docker service rm cluster_storm-ui cluster_storm-supervisor cluster_storm-nimbus-2 cluster_storm-nimbus-1
}

delete_hazelcast(){
    # hazelcast
    docker service rm cluster_hazelcast
}

delete_mongo(){
    # mongo
    docker service rm cluster_mongo cluster_mongo-express
}

delete_mysql(){
    # mysql
    docker service rm cluster_mysql-master cluster_mysql-slave
}

delete_fastdfs(){
    docker service rm \
      cluster_fdfs-tracker-2
}

delete_redis(){
    # redis
    docker service rm cluster_redis
}

service_status(){
    docker stack ps cluster | grep $1
    docker service ls | grep $1
}

swarm_usage(){
    cat <<-EOF

       Deploy: $0 deploy {uaa|hadoop|hbase|opentsdb|kafka|storm|hazelcast|mongo|mysql}     # deploy service
       Delete: $0 delete {uaa|hadoop|hbase}     # delete service
       Status: $0 status {uaa|hadoop|hbase}     # status service
       Images: $0 image {clean|pull}                  # image pull/clean

       docker stack ps cluster && docker service ls

EOF
    exit 0
}

clean_image(){
    for node in node01 node02 node03; do
        ssh -T $node 'docker system prune --all -f'
    done
}

pull_image(){
    # set -x
    list="$(grep -rn 'image:' /root/mango-swarm/deploy | grep -v deploy.sh |  awk '{print $NF}' | uniq | xargs)"
    for node in node01 node02 node03; do
        ssh -T $node <<-EOF
        for i in $list;do
          echo "[INFO] \$HOSTNAME docker pull \$i"
          docker pull \$i; 
        done
EOF
    done
}

# $2 null exit
[ -n "$2" ] || swarm_usage

case $1 in
    deploy)
        deploy_${2}
        ;;
    delete)
        delete_${2}
        ;;
    ps|status)
        service_status $2 
        ;;
    image)
        ${2}_image
        ;; 
    *)
        swarm_usage
        ;;
esac
